package com.ling.recipeassistant.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material.icons.filled.Schedule
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.ling.recipeassistant.R
import com.ling.recipeassistant.data.model.Recipe

@Composable
fun RecipeCard(
    recipe: Recipe,
    onClick: () -> Unit,
    onFavoriteClick: (() -> Unit)? = null
) {
    val context = LocalContext.current
    
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .clickable { onClick() },
        shape = RoundedCornerShape(12.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
    ) {
        Column {
            // Recipe Image
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(200.dp)
            ) {
                // 检查是否为本地图片资源
                if (recipe.imageUrl.endsWith(".png") || recipe.imageUrl.endsWith(".jpg") || recipe.imageUrl.endsWith(".jpeg")) {
                    // 本地图片资源
                    val resourceId = getLocalImageResourceId(recipe.imageUrl)
                    if (resourceId != 0) {
                        androidx.compose.foundation.Image(
                            painter = painterResource(id = resourceId),
                            contentDescription = recipe.name,
                            modifier = Modifier.fillMaxSize(),
                            contentScale = ContentScale.Crop
                        )
                    } else {
                        // 如果找不到本地资源，使用占位符
                        androidx.compose.foundation.Image(
                            painter = painterResource(id = R.drawable.recipe_placeholder_1),
                            contentDescription = recipe.name,
                            modifier = Modifier.fillMaxSize(),
                            contentScale = ContentScale.Crop
                        )
                    }
                } else {
                    // 网络图片（保留原有逻辑作为备用）
                    AsyncImage(
                        model = ImageRequest.Builder(context)
                            .data(recipe.imageUrl)
                            .crossfade(true)
                            .build(),
                        contentDescription = recipe.name,
                        modifier = Modifier.fillMaxSize(),
                        contentScale = ContentScale.Crop
                    )
                }
                
                // Favorite Button
                if (onFavoriteClick != null) {
                    IconButton(
                        onClick = onFavoriteClick,
                        modifier = Modifier
                            .align(Alignment.TopEnd)
                            .padding(8.dp)
                    ) {
                        Icon(
                            imageVector = if (recipe.isFavorite) Icons.Default.Favorite else Icons.Default.FavoriteBorder,
                            contentDescription = "收藏",
                            tint = if (recipe.isFavorite) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface
                        )
                    }
                }
            }
            
            // Recipe Info
            Column(
                modifier = Modifier.padding(16.dp)
            ) {
                Text(
                    text = recipe.name,
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.Bold,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis
                )
                
                Spacer(modifier = Modifier.height(4.dp))
                
                Text(
                    text = recipe.description,
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis
                )
                
                Spacer(modifier = Modifier.height(8.dp))
                
                // Recipe Meta Info
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.spacedBy(4.dp)
                    ) {
                        Icon(
                            imageVector = Icons.Default.Schedule,
                            contentDescription = null,
                            modifier = Modifier.size(16.dp),
                            tint = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                        Text(
                            text = "${recipe.cookTime}分钟",
                            style = MaterialTheme.typography.bodySmall,
                            color = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                    }
                    
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.spacedBy(4.dp)
                    ) {
                        Icon(
                            imageVector = Icons.Default.Star,
                            contentDescription = null,
                            modifier = Modifier.size(16.dp),
                            tint = MaterialTheme.colorScheme.tertiary
                        )
                        Text(
                            text = String.format("%.1f", recipe.rating),
                            style = MaterialTheme.typography.bodySmall,
                            color = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                    }
                }
            }
        }
    }
}

// 辅助函数：根据图片名称获取本地资源ID
private fun getLocalImageResourceId(imageName: String): Int {
    return when (imageName) {
        "wuhuarou.png" -> R.drawable.wuhuarou
        "mapodoufu.png" -> R.drawable.mapodoufu
        "tangculiji.png" -> R.drawable.tangculiji
        "gongbaojiding.png" -> R.drawable.gongbaojiding
        "baiqieji.png" -> R.drawable.baiqieji
        "chashaorou.png" -> R.drawable.chashaorou
        "fanqiedantang.png" -> R.drawable.fanqiedantang
        "dongguapaigutang.png" -> R.drawable.dongguapaigutang
        "yidalimian.png" -> R.drawable.yidalimian
        "niupai.png" -> R.drawable.niupai
        "pisa.png" -> R.drawable.pisa
        "qiaokelidangao.png" -> R.drawable.qiaokelidangao
        "tilamisu.png" -> R.drawable.tilamisu
        "makalong.png" -> R.drawable.makalong
        "suanrongxilanhua.png" -> R.drawable.suanrongxilanhua
        "disanxian.png" -> R.drawable.disanxian
        "jiandansanmingzhi.png" -> R.drawable.jiandansanmingzhi
        "yanmaizhou.png" -> R.drawable.yanmaizhou
        "ningmenfengmishui.png" -> R.drawable.ningmenfengmishui
        else -> R.drawable.recipe_placeholder_1 // 默认占位符
    }
} 